/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arg.deploy.webapp.template.ctrl;

import com.arg.deploy.webapp.common.MainPageController;
import com.arg.deploy.webapp.popup.ctrl.ChangePasswordCtrl;
import java.util.HashMap;
import java.util.Map;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;
import org.zkoss.zul.Window;

/**
 *
 * @author sorasaks
 */
public class UserMainCtrl extends MainPageController {

    private static final String FROM_POPUP_TEMPLATE = "/WEB-INF/layouts/defaults/popup/change-password.zul";
    private Button btnSignout;
    private Button btnPassword;

    public UserMainCtrl() {
        super("Deploy ::: User Control", Boolean.FALSE);
    }

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp); //To change body of generated methods, choose Tools | Templates.
        initailEventListener();
    }

    private void initailEventListener() {
        btnSignout.addEventListener(Events.ON_CLICK, new Signout());
        btnPassword.addEventListener(Events.ON_CLICK, new ChangePassword());
    }
    
    private class ChangePassword implements EventListener {
        
        @Override
        public void onEvent(Event t) throws Exception {
            
            Map map = new HashMap<String, Object>();
            map.put(ChangePasswordCtrl.EXECUTION_USER, userByLogin);
            Component comp = Executions.createComponents(FROM_POPUP_TEMPLATE, null, map);

            if (comp instanceof Window) {
                Window win = (Window) comp;
                win.setWidth("350px");
                win.setTitle("Deploy ::: Change password");
                win.doModal();
            }
        }
    }

    private class Signout implements EventListener {

        @Override
        public void onEvent(Event t) throws Exception {
            Executions.getCurrent().getSession().setAttribute(USER_PERMISSION, null);
            Executions.sendRedirect("/");
        }
    }
}
